﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Validation;

namespace Dycox.Workflows.Conditions
{
    public class RangeCondition : PropertyValueCondition
    {
        public RangeCondition(Dictionary<string, string> parameters)
            : base(parameters)
        {
            if (!parameters.TryGetValue("lowerBound", out _lowerBound) ||
                !parameters.TryGetValue("upperBound", out _upperBound))
                throw new InvalidOperationException("Either lower bound or upper bound value not set.");

            string value;

            if (parameters.TryGetValue("excludeBoundValues", out value))
            {
                bool exclude;
                
                if (bool.TryParse(value, out exclude))
                    _excludeBoundValues = exclude;
            }
        }

        private string _lowerBound, _upperBound;

        private bool _excludeBoundValues;

        protected override Validation.Validator CreateInnerValidator()
        {
            return new RangeValidator();
        }

        protected override void InitializeInnerValidator(Validator validator, object valueToValidate)
        {
            Type type = valueToValidate.GetType();
            RangeValidator rv = (RangeValidator)validator;

            rv.LowerBound = Dycox.Utils.ConvertUtil.ConvertType(_lowerBound, type);
            rv.UpperBound = Dycox.Utils.ConvertUtil.ConvertType(_upperBound, type);
            rv.ExcludeBoundValues = _excludeBoundValues;
        }
    }
}
